var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function (err, conn) {

    if (err) {exit(err);}
    conn.createChannel(function (err, ch) {
        if (err) { exit(err); }

        var ex = 'logs';
        var msg = process.argv.slice(2).join(' ') || 'Hello World';

        ch.assertExchange(ex, 'fanout', { durable: false });

        ch.assertQueue('', {exclusive: true}, function (err, q) {
            if (err) { exit(err); }

            console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
            
            ch.bindQueue(q.queue, ex, '');

            ch.consume(q.queue, function (msg) {
                console.log(" [x] %s", msg.content.toString());
            }, {noAck: true});
        });
    });
});

function exit(err) {
    console.log(err);
    process.exit(0);
}